package com.ktjy.springsecuitydemo.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder pw;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1.判断用户名是否存在，如果不存在，抛出 UsernameNotFoundException异常
        if (!"admin".equals(username)) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        // 2.把查询出来的密码（注册时已经加密过的），直接放入 User的构造方法中
        String password = pw.encode("111");
        return new User(username, password,
                //        AuthorityUtils.commaSeparatedStringToAuthorityList("admin,admiN"));
                AuthorityUtils.commaSeparatedStringToAuthorityList //ROLE_开头为角色权限
                        ("admin,/insert,/delete,ROLE_abc"));
    }
}
